{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "47b6ba3d",
   "metadata": {},
   "source": [
    "## 语音合成\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "57867fb4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备百度key和secret\n",
    "API_KEY = 'smxkOHWjqLVljEmIry5vuSYI'\n",
    "SECRET_KEY = 'ky3FZeSSDurxLyLLqZ2kPaEnts9NH1W1'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7e0de36e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from baidu_speech import fetch_token,asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "25c652f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.b7e89c1cdd913400bbd410388bdfc90f.2592000.1655988098.282335-19331335'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xu_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "xu_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2f293c93",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "# 通过调用电脑的麦克风进行音频的获取\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "491c3d0d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.167107\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'你好。'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "asr_result = eval(asr(xu_token,'1.wav'))['result'][0]\n",
    "asr_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f419a0b0",
   "metadata": {},
   "source": [
    "# 连接图灵机器人\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6e327970",
   "metadata": {},
   "outputs": [],
   "source": [
    "TL_key = \"d5d5bb66f66d4da8bedb378df7adfa70\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "42303c86",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests,json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "867f9c1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# json.dumps 将 python中的 dict ---> json\n",
    "# json.loads 将 json ---> python中的 dict "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c2dbe64b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'> {'reqType': 0, 'perception': {'inputText': {'text': '火龙果用英文怎么说'}}, 'userInfo': {'apiKey': 'd5d5bb66f66d4da8bedb378df7adfa70', 'userId': '000001'}}\n",
      "<class 'str'> {\"reqType\": 0, \"perception\": {\"inputText\": {\"text\": \"\\u706b\\u9f99\\u679c\\u7528\\u82f1\\u6587\\u600e\\u4e48\\u8bf4\"}}, \"userInfo\": {\"apiKey\": \"d5d5bb66f66d4da8bedb378df7adfa70\", \"userId\": \"000001\"}}\n"
     ]
    }
   ],
   "source": [
    "TL_url = 'http://openapi.turingapi.com/openapi/api/v2'\n",
    "\n",
    "params = {\n",
    "    \"reqType\":0,\n",
    "    \"perception\": {\n",
    "        \"inputText\": {\n",
    "            \"text\": \"火龙果用英文怎么说\"\n",
    "        },\n",
    "    },\n",
    "    \"userInfo\": {\n",
    "        \"apiKey\": TL_key,\n",
    "        \"userId\": \"000001\"\n",
    "    }\n",
    "}\n",
    "\n",
    "print(type(params),params)\n",
    "print(type(json.dumps(params)),json.dumps(params))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "26a6b3e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "res = requests.post(TL_url,data = json.dumps(params))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "37ee250a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "fc533f78",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'pitaya'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res.json()['results'][0]['values']['text']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "15f9d4e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'好吧，你也好。'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from tuling import TuLing\n",
    "TuLing(TL_key,'你好')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ace3ae9b",
   "metadata": {},
   "source": [
    "# 语音合成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b3bb9083",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/xuzhichao/anaconda3/lib/python3.7/site-packages/pydub/utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work\n",
      "  warn(\"Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work\", RuntimeWarning)\n"
     ]
    }
   ],
   "source": [
    "from pydub import AudioSegment # 播放音频模块\n",
    "from pydub.playback import play # 播放音频模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "407077a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from baidu_speech import tts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "66f8217d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "result saved as :result.wav\n"
     ]
    }
   ],
   "source": [
    "tts(xu_token,\"欢迎使用百度语音合成\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "10f83437",
   "metadata": {},
   "outputs": [],
   "source": [
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb9e1258",
   "metadata": {},
   "source": [
    "# 自动对话机器人（语音实现）\n",
    "\n",
    "> 1. 我们讲话，录制音频，存放在‘1.wav’测试文件中\n",
    "> 2. 语音识别：百度asr接口实现语音识别功能，将音频转换未文本\n",
    "> 3. 接入图灵机器人，实现文本回复\n",
    "> 4. 语音合成：百度tts接口实现语音合成功能，将回复的文本转换成音频\n",
    "> 5. 通过 python play模块进行播放"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "29662a4b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.194357\n",
      "广州:周二 05月24日,雷阵雨转中雨 持续无风向,最低气温24度，最高气温29度。\n",
      "result saved as :result.wav\n"
     ]
    }
   ],
   "source": [
    "import speech_recognition # 录制音频模块\n",
    "from baidu_speech import fetch_token,asr,tts # 百度语音系统模块\n",
    "from tuling import TuLing # 图灵机器人\n",
    "from pydub import AudioSegment # 播放音频模块\n",
    "from pydub.playback import play # 播放音频模块\n",
    "\n",
    "# 1. 我们讲话，录制音频，存放在‘1.wav’测试文件中\n",
    "r = speech_recognition.Recognizer()\n",
    "# 通过调用电脑的麦克风进行音频的获取\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "    \n",
    "# 2. 语音识别：百度asr接口实现语音识别功能，将音频转换未文本\n",
    "xu_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "asr_result = eval(asr(xu_token,'1.wav'))['result'][0]\n",
    "\n",
    "# 3. 接入图灵机器人，实现文本回复\n",
    "TL_result = TuLing(TL_key,asr_result)\n",
    "print(TL_result)\n",
    "\n",
    "# 4. 语音合成：百度tts接口实现语音合成功能，将回复的文本转换成音频\n",
    "tts(xu_token,TL_result)  \n",
    "\n",
    "# 5. 通过 python play模块进行播放\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ddad52d2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}